package com.rgs.gateway.common.enums;

import lombok.Getter;

import java.util.Objects;

/**
 * @author RenGaoshuai
 * @date 2024/7/1 13:32
 * @description
 */
@Getter
public enum SystemErrorEnum {
    DUPLICATE_PRIMARY_KEY(309, "唯一键冲突"),
    NOT_VALID(400, "客户端发送的请求无效"),
    UNAUTHORIZED(401, "未提供有效身份验证凭据"),
    NOT_PERMISSION(403, "客户端没有权限访问"),
    NOT_FOUND_SERVICE(404, "请求资源未找到"),
    NOT_VALID_REQUEST_METHOD(405, "客户端请求方式无效"),
    NOT_MATCH_AUTH_RULE(406, "验证信息格式或规则不匹配"),
    NOT_FOUND_TIMESTAMP(407, "时间戳为空"),
    AUTH_ERROR(409, "验证信息失败"),
    UPLOAD_FILE_SIZE_LIMIT(413, "上传文件大小超过限制"),
    ARGUMENT_NOT_VALID(422, "验证信息无效或无法处理"),
    SYSTEM_ERROR(500, "系统异常"),
    SYSTEM_BUSY(503, "系统繁忙,请稍候再试"),
    GATEWAY_ERROR(502, "网关异常"),
    CONNECT_TIME_OUT(504, "请求超时"),
    SIGN_TYPE_NOT_FUND(4011, "未知的签名类型"),


    ;

    /**
     * 错误类型码
     */
    private Integer code;
    /**
     * 错误类型描述信息
     */
    private String message;

    SystemErrorEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public static SystemErrorEnum getInstance(Integer code) {
        SystemErrorEnum[] values = SystemErrorEnum.values();
        for (SystemErrorEnum systemErrorType : values) {
            if (Objects.equals(code,systemErrorType.code)) {
                return systemErrorType;
            }
        }
        return SystemErrorEnum.SYSTEM_ERROR;
    }

    public static String getmesg(Integer code) {
        SystemErrorEnum exceptionEnum = getInstance(code);
        if (exceptionEnum == null) {
            return "未知异常";
        }
        return exceptionEnum.getMessage();
    }
}
